<template>
  <van-form @submit="onSubmit">
    <van-field
      v-model="password"
      type="password"
      name="password"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写需要修改的密码' }]"
    />
    <van-field
      v-model="secret_answer"
      name="secret_answer"
      label="验证信息"
      placeholder="验证信息"
      :rules="[{ required: true, message: '请填验证信息' }]"
    />
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit">
        修改
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
import { updatePassword } from "@/api/user";
import { removeToken } from "@/utils/token.js";
import router from "@/router";
import { onMounted, ref } from "vue";
import { showSuccessToast } from "vant";

let password = ref("");
let secret_answer = ref("");

let onSubmit = (value) => {
  updatePassword({
    password: value.password,
    secret_answer: value.secret_answer,
  })
    .then((res) => {
      showSuccessToast("修改成功");
      removeToken();
      router.replace("/login");
    })
    .catch((err) => {
      console.log(err);
    });
};
</script>